package com.longma.server.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longma.server.entity.Yuncang;
import com.longma.server.pojo.UserResponseEntity;
import com.longma.server.pojo.dto.PageRequestDTO;
import com.longma.server.pojo.dto.YuncangAddDTO;
import com.longma.server.pojo.dto.YuncangUpdateDTO;
import com.longma.server.pojo.vo.YuncangStatisticDataVO;
import com.longma.server.service.IYuncangService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HAIBO
 * @since 2021-04-28
 */
@RestController
@Validated
@RequestMapping("/api/yuncang")
public class YuncangController {

    @Autowired
    private IYuncangService yuncangService;

    @ApiOperation(value = "云仓注册")
    @PostMapping("/add")
    public UserResponseEntity<Yuncang> addYuncang(@Valid @RequestBody YuncangAddDTO addRequestDTO) {
        return UserResponseEntity.success(yuncangService.addYuncang(addRequestDTO));
    }

    @ApiOperation(value = "根据云仓id删除云仓")
    @PostMapping("/dellist")
    public UserResponseEntity<Boolean> deleteByIds(@RequestParam(name="ids") @Size(min = 1,message = "云仓id列表不能为空")
                                                               List<@Valid @NotNull(message = "云仓id不能为空") Integer> ids){
        return UserResponseEntity.success(yuncangService.deleteYuncangByIds(ids));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public UserResponseEntity<Yuncang> update(@Valid @RequestBody YuncangUpdateDTO updateRequestDTO) {
        return UserResponseEntity.success(yuncangService.updateYuncang(updateRequestDTO));
    }

    @ApiOperation(value = "通过云仓id查询")
    @GetMapping("/get")
    public UserResponseEntity<Yuncang> getYuncang(@NotNull(message = "云仓id不能为空")  @RequestParam(name = "id") Integer id){
        return UserResponseEntity.success(yuncangService.getYuncang(id));
    }

    @ApiOperation(value = "根据云仓id列表获取云仓运行统计数据  总发电量、总用电量、告警数等")
    @GetMapping("/statistic/get")
    public UserResponseEntity<Map<Integer, YuncangStatisticDataVO>> getYuncangStatisticData(@RequestParam(name="ids") @Size(min = 1,message = "云仓id列表不能为空")
                                                                                List<@Valid @NotNull(message = "云仓id不能为空") Integer> ids){
        return UserResponseEntity.success(yuncangService.getYuncangStatisticData(ids));
    }

    @ApiOperation(value = "获取所有云仓(分页)")
    @PostMapping("/list")
    public UserResponseEntity<IPage<Yuncang>> getByPage(@ApiParam(value = "分页查询，分页参数",required = true) @Valid @RequestBody PageRequestDTO pageRequestDTO){
        return UserResponseEntity.success(yuncangService.getYuncangByPage(pageRequestDTO.getPageNum(),
                pageRequestDTO.getPageSize(),pageRequestDTO.getAll(),pageRequestDTO.getConditions()));
    }

    @ApiOperation(value = "根据统计量、区域分页查询 获取云仓信息")
    @GetMapping("/listbystatistic")
    public UserResponseEntity<IPage<Yuncang>> getYuncangByPage(@ApiParam(value = "页码") @Min(value = 1,message = "页码最小值为1") @RequestParam(name = "pageNum") Long pageNum,
                                                               @ApiParam(value = "页大小") @Min(value = 1,message = "页大小最小为1") @RequestParam(name = "pageSize")  Long pageSize,
                                                               @ApiParam(value = "省份") @RequestParam(name = "province",required = false) String province,
                                                               @ApiParam(value = "城市") @RequestParam(name = "city",required = false) String city,
                                                               @ApiParam(value = "排序字段,支持consumption、generation、alarmCount") @RequestParam(name = "sortBy",required = false) String sortBy,
                                                               @ApiParam(value = "排序方式,支持asc、desc") @RequestParam(name = "order",required = false) String order){
        return UserResponseEntity.success(yuncangService.getYuncangByPage(pageNum,
                pageSize,province,city,sortBy,order));
    }
}

